package com.yungam.other.iot_controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.yungam.common.bean.equip.Equipment;
import com.yungam.common.bean.product.ProductionBoard;
import com.yungam.common.response.RetDTO;
import com.yungam.common.service.EquipmentService;
import com.yungam.common.service.ProductionBoardService;
import com.yungam.influxdb.service.CollectInfluxService;
import com.yungam.other.service.MesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 打磨工序结束
 */
@Slf4j
@RestController
@RequestMapping("/imcs_to_iot")
public class GrindProcessEndController {

    @Resource
    private EquipmentService equipmentService;
    @Resource
    private CollectInfluxService collectInfluxService;
    @Resource
    private MesService mesService;
    @Resource
    private ProductionBoardService productionBoardService;

    @PostMapping("/grindProcessEnd")
    public RetDTO<?> grindProcessEnd(@RequestBody Map<String, String> data) throws ParseException {
        log.info("========= START『iot调用imcs』[打磨工序结束接口]=========");
        String json = new Gson().toJson(data);
        log.info("=========『iot数据』{}", json);
        Map<String, Object> ans; // 返回数据
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String, Object> params = Maps.newHashMap();

        // 板材打磨设备
        Equipment equipment = equipmentService.getOne(new QueryWrapper<Equipment>()
                .eq("equ_ip", "173.10.0.2"));

        if (equipment == null) {
            log.error("=========『板材打磨设备不存在』=========");
            return RetDTO.error("板材打磨设备不存在");
        }

        // iot 参数
        String uid = data.get("uid");
        String startTime = data.get("startTime"), endTime = data.get("endTime");
        long start = sdf.parse(startTime).getTime() / 1000, end = sdf.parse(endTime).getTime() / 1000;
        Long[] range = { start, end };

        // influxdb
        List<Map<String, Object>> preprocessData = collectInfluxService
                .getCollectData(equipment.getEquIp(), range, null);

        // 整合 mes 接口需要的参数
        params.put("uid", uid);
        params.put("processType", 1);
        params.put("frNo", equipment.getFrNo());
        params.put("eqptNo", equipment.getEquNumber());
        params.put("beginTime", sdf.parse(startTime).getTime());
        params.put("endTime", sdf.parse(endTime).getTime());
        params.put("preprocessData", preprocessData);
        log.info("调用mes接口『preprocess』");
        log.info("给Mes发送的数据为{}", new Gson().toJson(params));
        ans = mesService.preprocess(params);

        log.info("调用mes接口『preprocess』『成功』, 返回的数据为{}", new Gson().toJson(ans));
        if (ans == null) return RetDTO.error("调用mes接口『preprocess』, 返回的数据为null");
        String code = String.valueOf(ans.get("code"));
        if (Objects.equals(code, "0")) { // 成功则保存
            log.info("插入 production_board");
            // 插入 production_board
            ProductionBoard pb = new ProductionBoard();
            pb.setUid(uid);
            pb.setStartTime(sdf.parse(startTime));
            pb.setEndTime(sdf.parse(endTime));
            pb.setState(1);
            productionBoardService.save(pb);
        }
        log.info("========= 给IOT返回数据为{} =========", new Gson().toJson(ans));
        log.info("========= END『iot调用imcs』[打磨工序结束接口] 调用成功 =========");
        return RetDTO.data(ans);
    }

}
